package  
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import com.greensock.*;
	
	
	/**
	 * ...
	 * @author KhanhNHV
	 */
	public class SquareText extends MovieClip
	{
		public var text: TextField;
		public var square:Square;
		
		//State
		//0: Not Selected
		//1: Selected 
		//2: Waiting
		//3: Opened
		//4: Closed
		public var state: Number = 0;
		public var isSpecialBlock: Boolean = false;				
		
		public function SquareText(txt:String,size:Number , isSpecial:Boolean) 
		{
			//textformat
			var textFormat:TextFormat
			if (isSpecial)
			{
				textFormat= new TextFormat("Arial", 30, 0xffffff, TextFormatAlign.CENTER);
			}else
			{
				textFormat= new TextFormat("Arial", 30, 0xff00ff, TextFormatAlign.CENTER);
			}
			
			text = new TextField();
			text.defaultTextFormat = textFormat;
			text.text = txt;
			text.alpha = 0;
			isSpecialBlock = isSpecial;
			text.width = size;
			text.height = size
			square = new Square(size,size,4, isSpecial);
			this.addChild(square);
			this.addChild(text);			
			this.alpha = 0.7;
			this.addEventListener(MouseEvent.CLICK, updateState);
			this.addEventListener(MouseEvent.MOUSE_WHEEL, updateState2);
		}
		
		public function updateState(me:MouseEvent):void 		
		{
			//in not selected state
			if (state == 0)
			{
				//check if is special block -- not specialblock
				if (!isSpecialBlock)
				{
					//raise event to parent to change state to focused
					dispatchEvent(new Event("OnGetFocused",true));
					
					//update state to getFocused
					state = 1;
					
				}else
				{
					//focus all special block
					parent.dispatchEvent(new Event("OnSpecialBlockFocused"));
				}
			}	
			
			//state is Waiting then change to 3 and reveal the word
			if (state == 2)
			{
				state = 3;
				
				//dispatch for parent to reveal others 
				dispatchEvent(new Event("OnCorrectAnswer", true));
			}
			
		}
		
		public function  updateState2(evy:Event):void 
		{
			//wrong then should change to disable
			if (state == 2)
			{
				dispatchEvent(new Event("OnWrongAnswer", true));
				
				//set state to closed
				state = 4;
			}
			
		}
		
		//reveal this 
		public function revealText(i:Number):void
		{
			TweenLite.to(this.text, 1.2, { delay:0.3 * i ,alpha:1} );
		}
		
		public function disabledText(i:Number):void
		{
			//TweenLite.to(this.square, 1.2, { delay:0.3 * i ,alpha:0.5} );
			this.square.disable();
			this.filters = [];
		}
		
		public function focusText(i:Number):void
		{
			TweenLite.to(this, 1.2, { alpha:1, delay:i * 0.1, onComplete:function() { var myGlowFilter:GlowFilter = new GlowFilter (0xFF00FF,1, 100, 100, 100, 3, false, true); this.filters = [myGlowFilter]; dispatchEvent(new Event("OnFocusTextCompleted", true)); } } );
		}
		
		public function waitingText():void
		{
			state = 2;
		}
		
		public function highlightSpecialBlock()
		{
			this.square.highlightSpecial();
		}
		
	}

}